#include "application.h"

#define DISPLAY_TASK_QUEUE_MAX	256 //64
OS_EVENT *DisplayTaskQ;
OS_EVENT *display_task_ack_m;

void *display_task_q_stack[DISPLAY_TASK_QUEUE_MAX];

static INT8U task_display_lock_st;
static INT8U task_display_lock_st_next;

extern void ap_update_last_filelist_ui_buf(void);
extern Overlap_Dispaly_Function Display;

static void task_display_lock_set(void)
{
	if(task_display_lock_st == task_display_lock_st_next)
	{
		return;
	}

	if(ap_display_rotate_busy())
	{
		return;
	}

	task_display_lock_st = task_display_lock_st_next;
}

static INT8U task_display_lock_get(void)
{
	if(ap_display_rotate_busy())
	{
		return 0;
	}
	else
	if(ap_display_queue_query(display_rotate_queue))
	{
		return 0;
	}
	else
	if(ap_display_queue_query(display_ready_queue))
	{
		return 0;
	}
	else
	{
		return task_display_lock_st;
	}
}

void task_display_lock_on(INT8U wait)
{
	OSQPost(DisplayTaskQ, (void *) MSG_DISPLAY_TASK_LOCK_ON);

	while(wait && !task_display_lock_get())
	{
		OSTimeDly(1);
	}
}

void task_display_lock_off(INT8U wait)
{
	OSQPost(DisplayTaskQ, (void *) MSG_DISPLAY_TASK_LOCK_OFF);

	while(wait && task_display_lock_get())
	{
		OSTimeDly(1);
	}
}

void task_display_init(void)
{
	DBG_PRINT("%s ... \r\n", __func__);

	DisplayTaskQ = OSQCreate(display_task_q_stack, DISPLAY_TASK_QUEUE_MAX);
	display_task_ack_m = OSMboxCreate(NULL);

	overlap_dispaly_function_initial();
	ap_display_init();
	ap_display_effect_init();
}

void task_display_entry(void *para)
{
	task_display_init();

	DBG_PRINT("%s ... \r\n", __func__);

	while(1)
	{
		INT8U	err;
		INT32U	msg = (INT32U) OSQPend(DisplayTaskQ, 0, &err);
		INT32U	msg_id;
		INT32U	para;

		if((!msg) || (err != OS_NO_ERR))
		{
			continue;
		}

		msg_id = msg & 0xFF000000;
		para = msg & 0x00FFFFFF;

		switch(msg_id)
		{
		case MSG_DISPLAY_TASK_EFFECT_INIT:
			ap_display_effect_init();
			break;

		case MSG_DISPLAY_TASK_JPEG_DRAW:
		case MSG_DISPLAY_TASK_MJPEG_DRAW:
		case MSG_DISPLAY_TASK_SETTING_DRAW:
		case MSG_DISPLAY_TASK_SENSOR_DRAW:
		case MSG_DISPLAY_TASK_BACK_A_CAR_DRAW:
		case MSG_DISPLAY_TASK_SETTING_SLIDE_DRAW:
			if(task_display_lock_st || task_display_lock_st_next)
			{
				ap_display_queue_put(display_free_queue, (DISP_P)para);
			}
			else
			{
				//DBG_PRINT("msg_id = 0x%x\r\n", msg_id);
				Display.ap_display_buff_copy_and_draw_ptr((DISP_P)para, msg_id);
			}
			break;
		#if C_FILE_LIST== TRUE
		case MSG_DISPLAY_TASK_File_list_UI_DRAW:
			ap_update_last_filelist_ui_buf();
			break;
		#endif
		case MSG_DISPLAY_TASK_ICON_SHOW:
			ap_display_icon_sts_set(para);
			break;

		case MSG_DISPLAY_TASK_ICON_CLEAR:
			ap_display_icon_sts_clear(para);
			break;

		case MSG_DISPLAY_TASK_LEFT_REC_TIME_DRAW:
			ap_display_left_rec_time_draw(para, 1);
			break;

		case MSG_DISPLAY_TASK_LEFT_REC_TIME_CLEAR:
			ap_display_left_rec_time_draw(0, 0);
			break;

		case MSG_DISPLAY_TASK_PROGRESS_BAR_SHOW:
			ap_display_progress_bar_type_set(para);
			break;

		case MSG_DISPLAY_TASK_STRING_DRAW:
			ap_display_string_draw((STR_ICON *) para);
			break;
			//add by zzy
		case MSG_DISPLAY_TASK_STRING_DRAW1:
			ap_display_string_draw1((STR_ICON *) para);
			break;
		case MSG_DISPLAY_TASK_STRING_DRAW2:
			ap_display_string_draw2((STR_ICON *) para);
			break;
	
		case MSG_DISPLAY_TASK_STRING_ICON_DRAW:
			ap_display_string_icon_start_draw((STR_ICON_EXT *) para);
			break;

		case MSG_DISPLAY_TASK_STRING_ICON_CLEAR:
			ap_display_string_icon_clear((STR_ICON_EXT *) para);
			break;
		case MSG_DISPLAY_TASK_ROTATE_DONE:
			ap_display_rotate_restart();
			task_display_lock_set();
			break;
		case MSG_DISPLAY_TASK_LOCK_ON:
			task_display_lock_st_next = 1;
			task_display_lock_set();
			break;
		case MSG_DISPLAY_TASK_LOCK_OFF:
			task_display_lock_st_next = 0;
			task_display_lock_set();
			break;
		}
	}
}
